home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / bastips2.zip / BASICMOD.TXT < prev    next >
Text File  |  1986-07-02  |  7KB  |  141 lines

  1.                      MODified Shapes for IBM
  2.          (COMPUTE! Magazine May 1986 by Paul W. Carlson)
  3.  
  4.      IBM BASIC's MOD operator is a powerful tool for performing
  5. certain arithmetic operations.  MOD gives the integer (whole number)
  6. remainder of an integer division.  For example, 17 MOD 3 = 2 because
  7. 17 divided by 3 equals 5 with a remainder of 2.  Likewise, 30 MOD 5
  8. = 0 since 5 divides into 30 evenly with a remainder of zero.
  9.      To see some applications of MOD arithmetic, run the programs
  10. below.  Each generates a different geometric display using MOD in
  11. various ways to simplify the graphics calculations.
  12.      Although some dialects of BASIC don't include a MOD operator,
  13. the INT function can be used for the same purpose.  In Microsoft
  14. BASIC, the expression K=INT(K/J)*J gives exactly the same result as
  15. the IBM BASIC expression K MOD J.
  16.      One of the most common uses of MOD is to test whether a value
  17. is odd or even.  The expression X MOD 2 yields a 1 if X is odd, or
  18. 0 if X is even.  Program 2 uses MOD for this purpose in line 20,
  19. branching to line 40 only when the variables I and J are both odd
  20. or even.
  21.      Another common use of MOD is to make a variable equal to a
  22. repeating sequence of consecutive integers.  Consider these statements:
  23.  
  24. 10 C=0
  25. 20 C=C MOD 3+1
  26. 30 PRINT C
  27. 40 GOTO 20
  28.  
  29. In this case the variable C cycles repeatedly through the series 1-2-3.
  30. Program 1 uses MOD in this manner to select the right color for each
  31. side of the rotating triangles.
  32.      MOD is also useful when arrays must be treated as circular rather
  33. than linear.  For example, say you have a numeric array X composed of
  34. three array elements, and that elements X(1), X(2), and X(3) contain
  35. the X coordinates for the vertices (corners) of a triangle.  In this
  36. case, if X(m) is the X coordinate for the beginning of a side, then
  37. the expression X(m MOD 3+1) gives the X coordinate for the end of that
  38. side.  This sort of expression appears in Programs 1, 2, and 3 which
  39. compute the variable NJ with MOD.  The result becomes an index into
  40. the arrays containing the vertex coordinats whenever the program needs
  41. to know the values for the end of a side.
  42.      After running the programs, you may want to know how to produce
  43. similar displays of your own.  The following explanation applies to
  44. Programs 1, 2, and 3, which have been made as similar as possible for
  45. comparison purposes.
  46.      The variable SU selects the spot on the side of a figure where
  47. the figure's corner will land after it is rotated and redrawn.  If
  48. you're not sure what the last sentence means, try changing SU to a
  49. slightly different value and rerunning the program.  You'll see
  50. exactly what it does.  The variables I and J represent the row and
  51. column of the figure.  The arrays X and Y contain the relative vertex
  52. coordinates for the current rotation.  The arrays XD and YD contain
  53. relative vertex coordinates for the next rotation.  N is the current
  54. rotation, M is the current side of a polygon, and NJ performs the
  55. function described above.
  56.      This is a line-by-line description of the various sections in
  57. each program:
  58.  
  59. Lines 10-50     Loop through J columns and I rows.  Skip over some
  60.                 combinations of I and J (Programs 1 and 3).  Change
  61.                 values in the Y array to plot some polygons upside-
  62.                 down (Programs 1 and 2).
  63.  
  64. Line 60         Loop through N rotations.  Compute the absolute
  65.                 coordinates of the first vertex.
  66.  
  67. Line 70         Loop through M sides.  Compute the absolute
  68.                 coordinates for the next vertex.
  69.  
  70. Line 80         Plot the side.  Make the absolute coordinates of the
  71.                 beginning of the next side equal to the absolute end
  72.                 coordinates of the side just plotted.
  73.  
  74. Line 90         Compute the relative coordinats of the vertex that
  75.                 fall on this side in the next rotation.
  76.  
  77. Line 100        Move the values from the array of relative coordinates
  78.                 for the next rotation into the array of relative
  79.                 coordinates for the next current rotation.
  80.  
  81.      Program 4 works somewhat differently from the other three.  After
  82. running this program, remove the statement C=1 from line 50 and run it
  83. again.  This time the colors take on a spiral pattern.
  84.  
  85. Program 1:
  86.  
  87. 10 SU=.1:RU=1-SU:KEY OFF:SCREEN 1:COLOR 0,1:II=1:C=1
  88. 20 FOR J=0 TO 3:II=-II:JJ=1:FOR I=0 TO 6:JJ=-JJ:IF I<J OR I>6-J THEN 110
  89. 30 IF J<2 OR I>2 THEN C=C MOD 3+1
  90. 40 IF J=3 THEN C=C MOD 3+1
  91. 50 X(1)=0:X(2)=39:X(3)=78:Y(1)=0:Y(3)=0:IF II=JJ THEN Y(2)=48 ELSE Y(2)=-48
  92. 60 FOR N=1 TO 11:X1=3+X(3)+I*39:Y1=175-Y(3)-J*48+II*JJ*24
  93. 70 FOR M=1 TO 3:X2=3+X(M)+I*39:Y2=175-Y(M)-J*48+II*JJ*24:C=C MOD 3+1
  94. 80 LINE(X1,Y1)-(X2,Y2),C:X1=X2:Y1=Y2:NJ=M MOD 3+1
  95. 90 XD(M)=RU*X(M)+SU*X(NJ):YD(M)=RU*Y(M)+SU*Y(NJ):NEXT M
  96. 100 FOR P=1 TO 3:X(P)=XD(P):Y(P)=YD(P):NEXT P,N
  97. 110 NEXT I,J
  98. 120 IF INKEY$="" THEN 120 ELSE CLS:SCREEN 0:WIDTH 80:KEY ON:END
  99.  
  100. Program 2:
  101.  
  102. 10 SU=.12:RU=1-SU:KEY OFF:CLS:SCREEN 1:COLOR 0,1
  103. 20 FOR I=0 TO 3:FOR J=0 TO 3:IF I MOD 2=J MOD 2 THEN 40
  104. 30 Y(1)=49:Y(2)=0:Y(3)=0:Y(4)=49:GOTO 50
  105. 40 Y(1)=0:Y(2)=49:Y(3)=49:Y(4)=0
  106. 50 X(1)=20:X(2)=20:X(3)=89:X(4)=89
  107. 60 FOR N=0 TO 18:X1=X(4)+I*69:Y1=Y(4)+J*49
  108. 70 FOR M=1 TO 4:X2=X(M)+I*69:Y2=Y(M)+J*49
  109. 80 LINE(X1,Y1)-(X2,Y2),M MOD 2+1:X1=X2:Y1=Y2:NJ=M MOD 4+1
  110. 90 XD(M)=RU*X(M)+SU*X(NJ):YD(M)=RU*Y(M)+SU*Y(NJ):NEXT M
  111. 100 FOR P=1 TO 4:X(P)=XD(P):Y(P)=YD(P):NEXT P,N,J,I
  112. 110 IF INKEY$="" THEN 110 ELSE CLS:SCREEN 0:WIDTH 80:END
  113.  
  114. Program 3:
  115.  
  116. 10 SU=.2:RU=1-SU:KEY OFF:CLS:SCREEN 1:COLOR 0,1
  117. 20 FOR J=0 TO 2:FOR I=0 TO 2:IF J=0 AND I<>1 THEN 110
  118. 30 IF I=1 THEN E=31 ELSE E=0
  119. 40 X(1)=0:X(2)=25:X(3)=75:X(4)=100:X(5)=75:X(6)=25
  120. 50 Y(1)=31:Y(2)=0:Y(3)=0:Y(4)=31:Y(5)=62:Y(6)=62
  121. 60 FOR N=0 TO 20:X1=35+X(6)+I*75:Y1=223-Y(6)-J*62-E
  122. 70 FOR M=1 TO 6:X2=35+X(M)+I*75:Y2=223-Y(M)-J*62-E
  123. 80 LINE(X1,Y1)-(X2,Y2),M MOD 3+1:X1=X2:Y1=Y2:NJ=M MOD 6+1
  124. 90 XD(M)=RU*X(M)+SU*X(NJ):YD(M)=RU*Y(M)+SU*Y(NJ):NEXT M
  125. 100 FOR P=1 TO 6:X(P)=XD(P):Y(P)=YD(P):NEXT P,N
  126. 110 NEXT I,J
  127. 120 IF INKEY$="" THEN 120 ELSE CLS:SCREEN 0:WIDTH 80:KEY ON:END
  128.  
  129. Program 4:
  130.  
  131. 10 KEY OFF:CLS:SCREEN 1:COLOR 0,0:FOR K=0 TO 9:READ F(K):NEXT
  132. 20 FOR A=10 TO 360 STEP 10
  133. 30 X=160+48*COS(A*.017453):Y=100+40*SIN(A*.017453)
  134. 40 CIRCLE(X,Y),60:NEXT
  135. 50 FOR A=10 TO 360 STEP 10:C=1:FOR K=0 TO 9:C=C MOD 3+1
  136. 60 X=160+1.2*F(K)*COS(A*.017453):Y=100+F(K)*SIN(A*.017453)
  137. 70 PAINT(X,Y),C,3:NEXT K,A
  138. 80 IF INKEY$="" THEN 80 ELSE CLS:SCREEN 0:WIDTH 80:KEY ON:END
  139. 90 DATA 26,34,41,51,61,68,78,84,87,89
  140.  
  141.